const { getList, getDetail, newBlog, updateBlog, deleteBlog } = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const handelBlogRouter = (req, res) => {
    const method = req.method


    //获取列表
    if( method === 'GET' && req.path === '/api/blog/list' ) {
        const author = req.query.author || ''
        const keyword = req.query.keyword || ''
        let result = getList(author, keyword)
        return result.then(listData => {
            return new SuccessModel(listData)
        })
    }

    //获取详情
    if( method === 'GET' && req.path === '/api/blog/detail' ) {
        const id = req.query.id || ''
        let result = getDetail(id)
        return result.then(info => {
            return new SuccessModel(info)
        })
    }

    //新建
    if( method === 'POST' && req.path === '/api/blog/new' ) {
        const author = 'liguansheng';
        const { title, content } = req.body;
        let result = newBlog(title, content, author)
        return result.then(id => {
            return new SuccessModel(id)
        })
    }

    //更新
    if( method === 'POST' && req.path === '/api/blog/update' ) {
        const author = 'liguansheng';
        const { id, title, content } = req.body
        let result = updateBlog( id, title, content )
        return result.then(row => {
            if(row > 0) {
                return new SuccessModel(true)
            }
        })
    }
    //删除
    if( method === 'POST' && req.path === '/api/blog/remove' ) {
        const author = 'liguansheng'
        const { id } = req.body
        let result = deleteBlog( id, author )
        return result.then(row => {
            if(row > 0) {
                return new SuccessModel(true)
            }
        })
    }
}

module.exports = handelBlogRouter